# -*- coding: utf-8 -*-

from random import *

from google.appengine.ext import webapp
from google.appengine.ext import db

from google.appengine.api import images

from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class Guestbook(db.Model):
	message = db.StringProperty(required=True)
	when = db.DateTimeProperty(auto_now_add=True)
	who = db.StringProperty()


class MainPage(webapp.RequestHandler):
    
    def get(self):
        self.response.out.write(template.render('templates/main.html',{"page":1}))
        
class News(webapp.RequestHandler):
    
    def get(self):
        self.response.out.write(template.render('templates/news.html',{"page":2}))
        
        
class Support(webapp.RequestHandler):
    
    def get(self):
    	posts = db.GqlQuery('SELECT * from Guestbook '
    						'ORDER BY when DESC')
    	values = {'posts': posts}
        self.response.out.write(template.render('templates/support.html',values,{"page":3}))
    def post(self):
    	post=Guestbook(message = self.request.get('message'),
    				who=self.request.get('who'))
    	post.put()
    	self.redirect('/support')    
        
class About(webapp.RequestHandler):
    
    def get(self):
        self.response.out.write(template.render('templates/about.html',{"page":4}))
      

		
class Redirect(webapp.RequestHandler):
	
	def get(self):
		
		#basico una lista static de paginas copadas
		leech_pages = [
			'http://www.xkcd.com',
			'http://www.crealotumismo.com',
			'http://www.pisotones.com',
			'http://www.7cerebros.com.ar',
			'http://www.danpat.fi/janne/flash/kuplamuovi.swf',
			'http://video.stumbleupon.com/#p=wqyzulwaph',
			'http://video.stumbleupon.com/#p=iujruwv2dz',
			'http://video.stumbleupon.com/#p=iuupvejamk',
			'http://video.stumbleupon.com/#p=hnu3jdx73y',
			'http://mshiltonj.com/software_wars/current/',
			'http://www.tinyvices.com/storms.html',
			'http://www.collegehumor.com',
			'http://www.guerrestellari.it/slas.html',
			'http://www.best4web.ch/fun/bacon_n_eggs.html',
			'http://www.echalk.co.uk/amusements/OpticalIllusions/illusions.htm',
			'http://svt.se/hogafflahage/hogafflaHage_site/Kor/hestekor.html',
			'http://rinkworks.com/said/courtroom.shtml',
			'http://www.phoenix-fly.com/videomenu/videogallerynew.htm',
			'http://www.flickr.com/photos/w00kie/sets/180637/show/',
			'http://www.seb.cc/particles/',
			'http://www.boingboing.net/2009/08/07/accused-florida-man.html',
			'http://www.pagetutor.com/idiot/idiot.html',
			'http://www.mindfake.com/',
			'http://www.angeloplessas.com/elasticenthusiastic/',
			'http://studenthome.nku.edu/~russelljo/flash/dudefalling.swf',
			'http://www.interbent.com/creatively-or-humorously-altered-signs/',
			'http://tul.com/',
			'http://survivingtheworld.net/Lesson1.html',
			'http://jpgmag.com/photos/1840098',
			'http://www.uncontrol.com/',
			'http://www.superbowl-ads.com/',
			'http://dl.ziza.ru/other/062009/22/pics/038_pics.jpg',
			'http://imgur.com/bGjGr.jpg',
			'http://freshpics.blogspot.com/2009/08/logo-hallucination-pattern-recognition.html',
			'http://www.brainyquote.com/quotes/topics/topic_computers.html',
			'http://www.crossover.dk/trace/',
			'http://www.zmescience.com/totally-awesome-natural-phenomena-you-probably-didnt-know-about-000033',
			'http://www.antilimit.com/',
			'http://www.youtube.com/watch?v=IyAyd4WnvhU',
			'http://j-walkblog.com/blog/docs/platform.htm',
			'http://selfdestruct.net/misc/geneve.jpg',
			'http://www.yugop.com/ver3/stuff/03/fla.html',
			'http://fun.drno.de/flash/HomeRun.swf',
			'http://blueballfixed.ytmnd.com/',
			'http://gizmodo.com/5330533/worlds-fastest-car-wash-has-its-disadvantages',
			'http://www.changar.com/archives/go.html',
			'http://www.vimeo.com/1700732',
			'http://www.specialdefects.com/v2/',
			'http://pascalcampion.com/door.swf',
			'http://netscrap.com/netscrap_detail.cfm?scrap_id=622',
			'http://www.buzzfeed.com/awesomer/first-person-shooter-disorder-cci',
			'http://www.maninthedark.com/',
			'http://www.bbc.co.uk/science/humanbody/sleep/sheep/',
			'http://www.xmarkjenkinsx.com/outside.html',
			'http://www.paintbits.com/desktop-customization/another-38-amazing-wallpapers/',
			'http://sprott.physics.wisc.edu/pickover/pc/manwoman.html',
			'http://www.rinkworks.com/stupid/cs_os.shtml',
			'http://antwrp.gsfc.nasa.gov/apod/image/0011/earthlights_dmsp_big.jpg',
			'http://www.collegehumor.com/video:1915202',
			'http://balldroppings.com/js/',
			'http://www.axess.com/twilight/console/',
			'http://uploads.ungrounded.net/221000/221483_Play.swf',
			'http://features.cgsociety.org/gallerycrits/60256/60256_1152359707_large.jpg',
			'http://www.clublaugh.com/es-items/712.swf',
			'http://majman.net/fly_loader.html'
			
			
			 # xD
		]
		
		#TODO: leer dinamicamente recomendaciones de digg y
		#       y otras paginas que se actualicen.
		
		dynamic = [
			'http://www.humorfeed.com/',
			'http://feeds.digg.com/digg/topic/comedy/popular.rss'
			]
		
		self.response.out.write("Redirecting to cool link...")
		self.redirect(choice(leech_pages))

class StyleShow(webapp.RequestHandler):
	
	def get(self):
		passed_values = {}
		self.response.out.write(template.render('templates/styleShow.html', passed_values ))

class Error(webapp.RequestHandler):
	
	def get(self):
		self.response.out.write(template.render('templates/error.html', {} ))
		
		
def main():
	
	app = webapp.WSGIApplication(
		[
		('/', MainPage),
        ('/about', About),
        ('/news', News),	
        ('/support', Support),
		('/test', Redirect),
		('/styleshow', StyleShow),
		('/.*' , Error),
		],
		debug = True)
		
	run_wsgi_app(app)
		
if __name__ == "__main__":
	main()